package com.example.exercise;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.exercise.MysqlUtil.DBService;

import java.util.ArrayList;

public class Auth extends AppCompatActivity {

    ImageView enter;
    EditText acc,pwd;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth);
        InitView();
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (acc.getText().toString().trim().equals("admin") || acc.getText().toString().trim().equals("user"))
                {
                    //开启验证线程
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String[] auth = new String[2];
                            auth = DBService.getAuth(acc.getText().toString().trim());
                            if (auth[0].equals(acc.getText().toString().trim()) && auth[1].equals(pwd.getText().toString().trim()))
                            {
                                Looper.prepare();
                                Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
                                editor.putString("Acc",acc.getText().toString().trim());
                                editor.apply();
                                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                                startActivity(intent);
                                Looper.loop();

                            }
                            else
                            {   Looper.prepare();
                                Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }

                        }
                    }).start();
                }else
                {
                    Toast.makeText(getApplicationContext(),"账号不存在",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void InitView() {
        enter = findViewById(R.id.enterauth);
        acc   = findViewById(R.id.account);
        pwd   = findViewById(R.id.pwd);
        sharedPreferences = getSharedPreferences("Acc",MODE_PRIVATE);
    }
}